package com.ycsj.goldmedalnewconcept.student.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.manager.PopUpManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.Res;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.student.activity.StudentAnalyzeActivity;
import com.ycsj.goldmedalnewconcept.student.bean.TableInfo;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherQuestionBankActivity;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherTkMainActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表格展示学生分析列表的适配器
 * Created by Administrator on 2018/10/9 0009.
 */

public class TableRvAdapter extends BaseQuickAdapter<TableInfo.DataBean, BaseViewHolder> {

    private Context context;
    private Activity activity;
    private List<TableInfo.DataBean> oldDatas;
    private String unit;
    private int postion;

    public TableRvAdapter(Context context, int postion, String unit, Activity activity, int layoutResId, @Nullable List<TableInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.oldDatas = data;
        this.activity = activity;
        this.unit = unit;
        this.postion = postion;
    }

    @Override
    protected void convert(BaseViewHolder helper, final TableInfo.DataBean item) {

        //线条
        View line = helper.getView(R.id.line);
        if (helper.getLayoutPosition() == 0) {
            line.setVisibility(View.GONE);
        } else {
            line.setVisibility(View.VISIBLE);
        }

        //排名
        TextView tvPm = helper.getView(R.id.tv_pm);
        tvPm.setText((helper.getLayoutPosition() + 1) + "");

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getName());

        //正确率
        TextView tvfs = helper.getView(R.id.tv_fs);

        //提交时间
        TextView tvTime = helper.getView(R.id.tv_tjsj);

        //错题数
        TextView tvCts = helper.getView(R.id.tv_cts);

        //查看试卷情况
        TextView tv_sjqk = helper.getView(R.id.tv_sjqk);

        //操作,有两种显示状态,1种是再次推送,蓝色字体,1种是催交练习,红色字体
        TextView tvCz = helper.getView(R.id.tv_cz);

        if ("1".equals(item.getIs_done())) {
            //已做
            tvfs.setText(item.getPercent());
            tvTime.setText(item.getDone_time());
            tvCts.setText(item.getWrong_amount() + "");
            tv_sjqk.setText("查看");
            tv_sjqk.setEnabled(true);
            tvCz.setText("再次推送");
            tvCz.setTextColor(context.getResources().getColor(R.color.zcts));

            tv_sjqk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到套题界面,老师查看某个学生和学生答题查看解析状态值一致
                    TeacherTkMainActivity.goActivity(context, item.getExam_id(), 1 + "", unit, "");
                }
            });

        } else {
            //未做
            tvfs.setText("-");
            tvTime.setText("-");
            tvCts.setText("-");
            tv_sjqk.setText("-");
            tv_sjqk.setEnabled(false);
            tvCz.setText("催交练习");
            tvCz.setTextColor(context.getResources().getColor(R.color.cjzy));
        }


        tvCz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("1".equals(item.getIs_done())) {
                    //弹推送框
                    showPushPop(item);
                } else {
                    //提交操作
                    doPushAction(item);
                    ToastUtils.showShort("已催促学生尽快完成练习");
                }
            }
        });
    }


    /**
     * 弹推送框
     */
    private void showPushPop(final TableInfo.DataBean item) {
        PopUpManager.showPop(context, R.layout.teacher_app_pop_push, 0.3f, activity, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //内容
                TextView tvContent = view.findViewById(R.id.tv_content);

                //提交
                TextView tvCommit = view.findViewById(R.id.tv_commit);

                tvCommit.setText("再次推送");
                tvContent.setText("是否确定再次推送题目给" + item.getName() + "吗?");

                //取消
                TextView tvCancel = view.findViewById(R.id.tv_cancel);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });


                tvCommit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                        //提交操作
                        doPushAction(item);
                    }
                });

            }
        });
    }


    /**
     * 再次推送
     */
    private void doPushAction(final TableInfo.DataBean item) {
        CustomDialogManager.show(context, "推送中...");
        Map<String, String> map = new HashMap<>();
        map.put("jobmsg", "8");
        map.put("examid", item.getExam_id());
        if ("1".equals(item.getIs_done())) {
            //已做
            //再次推送
            map.put("state", "1");
        } else if ("0".equals(item.getIs_done())) {
            //催交练习
            map.put("state", "0");
        }

        HttpUtils.post(HttpAddress.PUSHAGAIN, map, this, new HttpCallBack<Res>() {
            @Override
            public void onSuccess(Res info) {
                CustomDialogManager.dissmiss();
                if (null != info) {
                    if (1 == info.getState()) {
                        //返回成功

                        if ("1".equals(item.getIs_done())) {
                            //发送消息通知题库练习界面的条目刷新
                            BusInfo in = new BusInfo();
                            in.type = TeacherQuestionBankActivity.UPDATEPAGE;
                            //更新的位置
                            in.tkPos = postion;
                            //学生姓名
                            in.stuName = item.getName();
                            EventBus.getDefault().post(in);
                        }

                        //刷新页面数据
                        BusInfo busInfo = new BusInfo();
                        busInfo.type = StudentAnalyzeActivity.UPDATEPAGE;
                        EventBus.getDefault().post(busInfo);
                        Log.i("test", "examid=" + item.getExam_id());
                        Log.i("test", "推送成功");
                    } else {
                        ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
                        Log.i("test", Constants.ERRORSTATE + info.getState());
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }

            @Override
            public void onError(String e) {
                CustomDialogManager.dissmiss();
                ToastUtils.showShort("服务器异常,操作失败");
            }
        });
    }
}
